package com.ericsson.smart.model;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.annotate.JsonRawValue;

public class BreadCrumb implements Serializable {

	private static final long serialVersionUID = 7703181092920765899L;

	String dimension;
	
	Map<String, Object> dimensionValueMap;
	
	private Object metaData;

    @JsonRawValue
    public String getMetaData() {
        return metaData == null ? null : metaData.toString();
    }

    public void setMetaData(JsonNode node) {
        this.metaData = node;
    }

	public String getDimension() {
		return dimension;
	}

	public void setDimension(String breadCrumbDimension) {
		this.dimension = breadCrumbDimension;
	}
	
	public Map<String, Object> getDimensionValueMap() {
		return dimensionValueMap;
	}

	public void setDimensionValueMap(Map<String, Object> dimensionValueMap) {
		this.dimensionValueMap = dimensionValueMap;
	}
	
	public void setBreadCrumb(String breadCrumbDimension,Map<String, Object> dimensionValueMap) {
		setDimension(breadCrumbDimension);
		setDimensionValueMap(dimensionValueMap);
	}

	public void addValueToBreadCrumb(String dimensionLevelName, Object dimensionLevelValue) {
		if (dimensionValueMap == null) {
			dimensionValueMap = new LinkedHashMap<String, Object>();
		}
		//System.out.println("dimensionLevelName:"+dimensionLevelName);
		//System.out.println("dimensionLevelValue:"+dimensionLevelValue);
		//System.out.println("dimensionValueMap:"+dimensionValueMap);
		dimensionValueMap.put(dimensionLevelName.trim(), dimensionLevelValue);
	}

	public void updateBreadCrumb(Map<String, Object> dimensionValueMap) {
		this.dimensionValueMap.clear();
		setDimensionValueMap(dimensionValueMap);
	}
	
	public Object getValueFromBreadCrumb(String dimensionLevelName) {
		if (!dimensionValueMap.containsKey(dimensionLevelName)) {
			return null;
		} else {
			return dimensionValueMap.get(dimensionLevelName);
//			for (Entry<String, String> dimensionValue : dimensionValueMap.entrySet()) {
//				if (dimensionValue.getKey().equals(dimensionLevelName)) {
//					return dimensionValue.getValue();
//				}
//			}
//			return null;
		}
	}	

	public void updateBreadCrumb(String dimensionLevelName,String dimensionLevelValue) {
		addValueToBreadCrumb(dimensionLevelName, dimensionLevelValue);
	}	

	public Object removeFromBreadCrumb(String dimensionLevelName) {
		Object deletedValue = null;
		Map<String, Object> newDimensionValueMap = null;
		if (!dimensionValueMap.containsKey(dimensionLevelName)) {
			return null;
		} else {
			for (Entry<String, Object> dimensionValue : dimensionValueMap.entrySet()) {
				if (newDimensionValueMap == null) {
					newDimensionValueMap = new LinkedHashMap<String, Object>();
				}
				newDimensionValueMap.put(dimensionValue.getKey(), dimensionValue.getValue());		
				if (dimensionValue.getKey().equals(dimensionLevelName)) {
					deletedValue = dimensionValue.getValue();
					break;
				} 						
			}
			dimensionValueMap = newDimensionValueMap;
			return deletedValue;
		}
	}

	public String toString() {
		StringBuffer output = new StringBuffer();
		output.append("\n ---------BREAD-CRUMB---------");
		output.append("\n " + "DIMENSION=" + dimension); 
		if (dimensionValueMap == null) {
			output.append("NULL");
		} else {
			for (Entry<String, Object> dimensionValue : dimensionValueMap.entrySet()) {
				output.append("\n " + "LEVEL=" + dimensionValue.getKey() + " : VALUE="
						+ dimensionValue.getValue());
			}
		}
		return output.toString();
	}

}
